﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Alogient.Cameleon.Forms.Sdk.Util
{
    public class ConfigurationUtils
    {
        private ConfigurationUtils() { }

        private static string connectionString = null;
        private static bool? formManagerEnabled = null;
        private static string uploadPath = null;
        private static int? tempFolderTTL = null;
        private static int? defaultBlackListCount = null;
        private static string[] defaultBlackList;
        private static int? surveyResultDigits = null;

        public static string ConnectionString
        {
            get
            {
                if (FormManagerEnabled)
                {
                    if (connectionString == null)
                    {
                        connectionString = ConfigurationManager.ConnectionStrings[Constants.CONNECTION_STRING_NAME].ConnectionString;
                    }

                    return connectionString;
                }

                return string.Empty;
            }
        }

        public static bool FormManagerEnabled
        {
            get
            {
                if (formManagerEnabled == null)
                {
                    formManagerEnabled = ConfigurationManager.ConnectionStrings[Constants.CONNECTION_STRING_NAME] != null;
                }

                return formManagerEnabled.Value;
            }
        }

        public static string UploadPath
        {
            get
            {
                if (uploadPath == null)
                {
                    uploadPath = ConfigurationManager.AppSettings[Constants.UPLOAD_PATH];
                }

                return uploadPath;
            }
        }

        public static int TempFolderTTL
        {
            get
            {
                if (tempFolderTTL == null)
                {
                    int tmp;
                    tempFolderTTL = 24;
                    if (int.TryParse(ConfigurationManager.AppSettings[Constants.TEMP_FOLDER_TTL], out tmp))
                    {
                        tempFolderTTL = tmp;
                    }
                }

                return tempFolderTTL.Value;
            }
        }

        public static string[] DefaultBlackList
        {
            get
            {
                if (defaultBlackListCount == null)
                {
                    string s = ConfigurationManager.AppSettings[Constants.DEFAULT_BLACK_LIST];
                    defaultBlackList = s.ToLower().Split(',');
                    defaultBlackListCount = defaultBlackList.Length;
                }

                return defaultBlackList;
            }
        }

        public static int SurveyResultDigits
        {
            get
            {
                if (surveyResultDigits == null)
                {
                    int tmp;
                    surveyResultDigits = 0;
                    if (int.TryParse(ConfigurationManager.AppSettings[Constants.SURVEY_RESULT_DIGITS], out tmp))
                    {
                        surveyResultDigits = tmp;
                    }
                }

                return surveyResultDigits.Value;
            }
        }
    }
}
